package com.ddh.ddhsanguogame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddh.ddhsanguogame.constant.MessageTypeConstants;
import com.ddh.ddhsanguogame.constant.RedisConstant;
import com.ddh.ddhsanguogame.dto.CardAllianceDTO;
import com.ddh.ddhsanguogame.dto.CardMessageDTO;
import com.ddh.ddhsanguogame.entity.CardMessage;
import com.ddh.ddhsanguogame.entity.CardUser;
import com.ddh.ddhsanguogame.entity.CardAlliance;
import com.ddh.ddhsanguogame.entity.CardUserAlliance;
import com.ddh.ddhsanguogame.mapper.CardMessageMapper;
import com.ddh.ddhsanguogame.mapper.CardAllianceMapper;
import com.ddh.ddhsanguogame.mapper.CardUserAllianceMapper;
import com.ddh.ddhsanguogame.service.ICardMessageService;
import com.ddh.ddhsanguogame.utils.RedisService;
import com.ddh.ddhsanguogame.vo.CardMessageVO;
import com.ddh.ddhsanguogame.webSocket.entity.CardMessageRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author
 * @since 2025-06-29
 */
@Service
public class CardMessageServiceImpl extends ServiceImpl<CardMessageMapper, CardMessage> implements ICardMessageService {

    @Resource
    private RedisService redisService;

    @Resource
    private CardAllianceMapper cardAllianceMapper;

    @Resource
    private CardUserAllianceMapper cardUserAllianceMapper;

    @Override
    @SneakyThrows
    public List<CardMessageVO> getHistoryMessage(CardMessageDTO cardMessageDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        List<CardMessageVO> result = new ArrayList<>();
        String messageType = cardMessageDTO.getMessageType();
        Long senderId = cardMessageDTO.getSenderId();
        Long receiverId = cardMessageDTO.getReceiverId();
        switch (messageType) {
            case MessageTypeConstants.WORLD:
                List<Object> historyWorldList = redisService.getHistory(messageType, 0, 50);
                // 将从Redis中获取的对象列表转换为CardMessageVO对象列表
                for (Object obj : historyWorldList) {
                    // 修改：将 JSON 字符串反序列化为 CardMessageRequest 对象
                    String jsonStr = (String) obj;
                    CardMessageRequest request = objectMapper.readValue(jsonStr, CardMessageRequest.class);
                    result.add(convertToVO(request));
                }
                return result;
            case MessageTypeConstants.PRIVATE: {
                List<Object> historyPrivateList = redisService.getHistory(messageType, 0, 50);
                for (Object obj : historyPrivateList) {
                    String jsonStr = (String) obj;
                    CardMessageRequest request = objectMapper.readValue(jsonStr, CardMessageRequest.class);

                    // 双向匹配：A发给B 或 B发给A
                    if ((request.getSenderId().equals(senderId) && request.getReceiverId().equals(receiverId)) ||
                            (request.getSenderId().equals(receiverId) && request.getReceiverId().equals(senderId))) {
                        result.add(convertToVO(request));
                    }
                }
                return result;
            }

            default:
                return Collections.emptyList();
        }
    }

    private CardMessageVO convertToVO(CardMessageRequest request) {
        CardMessageVO vo = new CardMessageVO();
        BeanUtils.copyProperties(request, vo);
        return vo;
    }

}
